import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hx_base/pages/BaseTextField.dart';
import 'package:hx_base/utils/HXUtils.dart';
import 'package:hx_base/utils/LoggerUtil.dart';

class ListViewTextFieldPage extends StatelessWidget {
  ListViewTextFieldPage({super.key});

  var dataList = <InspectionItems>[];

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => HXUtils.hideKeyboard(),
      child: Scaffold(
        appBar: AppBar(
          title: Text("多个输入框"),
        ),
        body: bodyView(),
        bottomNavigationBar: ElevatedButton(
            onPressed: () {
              for (var element in dataList) {
                Log.d(element.toJson());
              }
            },
            child: Text("提交")),
      ),
    );
  }

  bodyView() {
    for (var i = 0; i < 5; i++) {
      InspectionItems items = InspectionItems(name: "第$i行", value: "1$i");
      Log.w(items.name);
      dataList.add(items);
    }
    return ListView.builder(
      itemBuilder: setItemBuilder,
      itemCount: dataList.length,
      padding: EdgeInsets.zero,
    );
  }

  Widget setItemBuilder(BuildContext context, int index) {
    InspectionItems item = dataList.elementAt(index);
    return Container(
      padding: EdgeInsets.symmetric(vertical: 5.h, horizontal: 5.w),
      margin: EdgeInsets.only(bottom: 5.h),
      decoration: BoxDecoration(
        color: Color(0xFFEAEAEA),
        borderRadius: BorderRadius.circular(5.r),
      ),
      child: Row(
        children: [
          Text(item.name ?? ""),
          Expanded(
            child: BaseTextField(
              text: item.value ?? "",
              textAlign: TextAlign.end,
              callBackValue: (value) => item.value = value,
            ),
          ),
        ],
      ),
    );
  }
}

class InspectionItems {
  String? key;
  String? name;
  String? value;

  InspectionItems({this.key, this.name, this.value});

  InspectionItems.fromJson(Map<String, dynamic> json) {
    key = json['key'];
    name = json['name'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['key'] = key;
    data['name'] = name;
    data['value'] = value;
    return data;
  }
}
